File Search Library
Getting Started VB.NET

Getting a list of files which contain the matching text

'Setup the search parameters

Dim parms As New SearchParms()

  'This is the directory to search

parms.DirectoriesToSearch.Add("c:\_git")

  'Include all C# files by wildcard

parms.FilesToInclude.Add("*.cs")

parms.FileIncludeSearchType = FileSearchType.Wildcard

 

'Search for a plain text phrase

parms.SearchString = "public partial class"

parms.SearchExpressionType = SearchExpressionType.PlainText

'Search with the parameters

Dim logic As New FileSearchLogic()

Dim results As List(Of String) = logic.SearchFiles(parms)

 

For Each filePath In results

Console.WriteLine(filePath)

Next filePath

 

Getting a list of locations in a file which contain the matching text

'Set the file to load

Dim filePath As String = "C:\_git\Compare-Net-Objects\Compare-NET-Objects-Tests\TestClasses\Invoice.cs"

 

'What to search in the file

Dim searchParms As New SearchParms()

searchParms.SearchString = "FirstName"

searchParms.SearchExpressionType = SearchExpressionType.PlainText

  'Load the matches for the file

Dim loadFileMatchesLogic As New LoadFileMatchesLogic()

Dim loadFileResult As LoadFileResult = loadFileMatchesLogic.LoadFileMatches(searchParms, filePath)

 

For Each matchLocation In loadFileResult.MatchLocations

Console.WriteLine($"{matchLocation.MatchText}: Start {matchLocation.StartPosition}, Length {matchLocation.Length}")

Next matchLocation

 

Creating and Searching an index

'Setup the search parameters

Dim searchParms As New SearchParms()

 

'This is the directory to index

searchParms.DirectoriesToSearch.Add("c:\_git")

  'Include all C# files by wildcard

searchParms.FilesToInclude.Add("*.cs")

searchParms.FileIncludeSearchType = FileSearchType.Wildcard

  'Set the directory to save the index

searchParms.IndexPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "FileSearchLibrarySearchIndexExample")

 

'Create the index

Dim indexLogic As New IndexLogic()

indexLogic.CreateIndexIfItDoesNotExist(searchParms)

 

'Specify what to search

searchParms.SearchString = "public partial class"

searchParms.SearchExpressionType = SearchExpressionType.PlainText

  'List the files that are found

Dim files As List(Of String) = indexLogic.SearchIndex(searchParms)

 

For Each file As String In files

Console.WriteLine(file)

Next file